//
//  PPAlbumModel.m
//  
//
//  Created by Jinxiao on 8/28/16.
//
//

#import "PPAlbumModel.h"

#import "PPImageTransaction.h"

@implementation PPAlbumModel

// Insert code here to add functionality to your managed object subclass
+ (instancetype)existingAlbumForUUID:(NSString *)uuid {
    if(uuid.length == 0) {
        return nil;
    }
    
    PPAlbumModel *album = [self find:@{@"auuid": uuid}];
    if(album == nil) {
        album = [PPAlbumModel create];
        album.auuid = uuid;
        album.deleted = @(NO);
        album.created = @(NO);
        album.modified = @(NO);
        [album.managedObjectContext saveContext];
    }

    return album;
}

- (NSString *)coverURLString {
    if(self.coverKey.length == 0 || self.coverHost.length == 0) {
        return nil;
    }

    return [NSString stringWithFormat:@"%@%@", self.coverHost, self.coverKey];
}

- (void)queryCoverImageWithCompletion:(void (^)(UIImage *, NSError *))completion {
    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        NSString *URLString = self.coverURLString;
        if(URLString.length > 0) {
            [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:URLString] options:kNilOptions progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                !completion ?: completion(image, error);
            }];
            return;
        }

        UIImage *cover = nil;

        NSArray <PPAlbumImageModel *> *images = [PPAlbumImageModel where:@{@"auuid" : self.auuid, @"deleted" : @(NO)}];

        for(PPAlbumImageModel *image in images) {
            if(image.isCover) {
                if(image.URLString.length > 0) {
                    URLString = image.URLString;
                } else {
                    cover = [[PPImageTransaction sharedInstance] imageForKey:image.uuid];
                }
                break;
            }
        }

        if(URLString.length > 0) {
            [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:URLString] options:kNilOptions progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                !completion ?: completion(image, error);
            }];
            return;
        }

        if(cover != nil) {
            !completion ?: completion(cover, nil);
            return;
        }

        !completion ?: completion(nil, [NSError errorWithDomain:@"com.pickpic.error" code:0 userInfo:@{NSLocalizedDescriptionKey: @"相册封面加载失败"}]);
    });
}



- (void)queryAlbumCoverImageWithCompletion:(void (^)(UIImage *,NSString *coverString, NSError *))completion {
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        NSString *URLString = self.coverURLString;
        
        if(URLString.length > 0) {
            [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:URLString] options:kNilOptions progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                !completion ?: completion(image,URLString, error);
            }];
            return;
        }
        
        UIImage *cover = nil;
        
        NSArray <PPAlbumImageModel *> *images = [PPAlbumImageModel where:@{@"auuid" : self.auuid, @"deleted" : @(NO)}];
        
        for(PPAlbumImageModel *image in images) {
            if(image.isCover) {
                if(image.URLString.length > 0) {
                    URLString = image.URLString;
                } else {
                    cover = [[PPImageTransaction sharedInstance] imageForKey:image.uuid];
                }
                break;
            }
        }
        
        if(URLString.length > 0) {
            [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:URLString] options:kNilOptions progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                !completion ?: completion(image,URLString, error);
            }];
            return;
        }
        
        if(cover != nil) {
            !completion ?: completion(cover,URLString, nil);
            return;
        }
        
        !completion ?: completion(nil,nil, [NSError errorWithDomain:@"com.pickpic.error" code:0 userInfo:@{NSLocalizedDescriptionKey: @"相册封面加载失败"}]);
    });
}
@end
